<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<%@ page import="com.josiejune.documentdispatch.models.DDMessage" %>
<%@ page import="com.josiejune.documentdispatch.models.DAO" %>
<%@ page import="com.josiejune.documentdispatch.models.Tag" %>
<%@ page import="com.josiejune.documentdispatch.models.Event" %>
<%@ page import="com.josiejune.documentdispatch.servlets.views.FormatUtil" %>
<%@ page import="com.googlecode.objectify.Key" %>
<%@ page import="java.util.List" %>
<%@ page import="com.josiejune.documentdispatch.models.DDMessage" %>
<%@ page import="com.josiejune.documentdispatch.models.DAO" %>
<%@ page import="com.josiejune.documentdispatch.models.Tag" %>
<%@ page import="com.josiejune.documentdispatch.models.Event" %>
<%@ page import="com.josiejune.documentdispatch.servlets.views.FormatUtil" %>
<%@ page import="com.googlecode.objectify.Key" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Set" %>

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<% 
	String msgId = request.getParameter("msgId");
%>
<title><%= msgId %>: Message Details</title> 
	
  <link rel="stylesheet" href="styles/stencil/screen.css" type="text/css" media="screen" charset="utf-8" /> 
  <link rel="stylesheet" href="styles/stencil/custom.css" type="text/css" media="screen" title="no title" charset="utf-8" /> 
  <link href="styles/stencil/lib/plugins/forms.css" rel="stylesheet" type="text/css" /> 
  <link href="styles/stencil/lib/plugins/buttons.css" rel="stylesheet" type="text/css" /> 
  <link href="styles/stencil/lib/plugins/grid.css" rel="stylesheet" type="text/css" /> 
  <link href="styles/stencil/lib/typography.css" rel="stylesheet" type="text/css" /> 

  <script type="text/javascript" src="http://www.google.com/jsapi"></script> 
  <script type="text/javascript"> 
 
  //Load the Visualization API and the ready-made Google table visualization
  google.load('visualization', '1', {'packages':['table']});
 
  // Set a callback to run when the API is loaded.
  google.setOnLoadCallback(init);
 
  // Send the query to the data source.
  function init() {
 
    // Specify the data source URL.
   var query = new google.visualization.Query('../eventlist?itemType=Message&itemId=<%=request.getParameter("msgId")%>');
  
    // Send the query with a callback function.
    query.send(handleQueryResponse);
  }
 
  // Handle the query response.
  function handleQueryResponse(response) {
    if (response.isError()) {
      alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
      return;
    }
 
    // Draw the visualization.
    var data = response.getDataTable();
    var chart = new google.visualization.Table(document.getElementById('chart_div'));
    chart.draw(data, {allowHtml: true, 'sortColumn':0, 'sortAscending':false});
  }
  </script> 

  </head> 
   <body id="index"> 
  	<div id="page" class="xgrid xfluid"> 
  	
  	<div id="header" class="x12">		
  		<div class="xpad"> 
  			<h1>Message Details: <%= msgId %></h1>		
  		</div> 
  	</div> 

<%@ include file="headermenu.jsp" %>
	<% 
	
	DAO dao = new DAO();
	DDMessage message = dao.ofy().get(new Key<DDMessage>(DDMessage.class, Long.valueOf(msgId)));
	String debugStr = request.getParameter("debug");
	boolean debug = false;
	if (debugStr != null) {
		debug=true;
	}
	
	%>

<br/>
<div class="field" class="x12">
	ID: <%= message.getId() %>
</div>

<div class="field" class="x12">
	<form action="/updatemessage?msgId=<%= URLEncoder.encode(msgId, "UTF-8") %>" method="post" class="stn-form label-inline simple"> 
	   
		
		<div class="field">
			Number of Orders
		<input id="workCount" name="workCount" size="5" type="text" value="<%=message.getWorkCount() %>"/>
</div>
<div class="field">
			Notes
		<input id="notes" name="notes" size="25" type="text" value="<%=message.getNotes() %>"/>
</div>
<div class="field">
			<input type="checkbox" name="managed" value="true" 
	<% if (message.isManaged()) { %>
		checked
	<% } %>
	/>Managed
</div>
	<input type="submit" value="Update" ></input>
		</form> 
	
</div>



	
	
<br/>
<div class="field">
	From: <%= message.getFrom() %>
</div>
<br/>
<div class="field">
	Subject: <%= message.getMsgSubject() %>
</div>
<br/>
<div class="field">
	Created: <%= FormatUtil.getDateTimeValue(message.getCreatedDate()).toString() %>
</div>
<br/>
<div class="field">
	Modified: <%= FormatUtil.getDateTimeValue(message.getModifiedTimestamp()).toString() %>
</div>
<br/>
<div class="field">
	Status: <%= message.getStatus() %>
</div>
<br/>
<div class="field">
	Keyword: <%= message.getKeyword() %>
</div>
<% 
List<String> filenames = message.getFilenames();	
for (int i=0; i< filenames.size(); i++){ 
	String filename = filenames.get(i);
%>
 	<div class="field">
	File:	<a href="/download?msgId=<%=URLEncoder.encode(String.valueOf(message.getId()), "UTF-8") %>&fileNum=<%=i %>" ><%= filename %></a>
<br/>
<% } %>

</div>
<br/>
<% 

List<Tag> tags = dao.getTagsForMessage(message);
for (Tag tag : tags){ 
%>
 	<div class="field">
	Tag:	<%= tag.getTag() %></div>
<br/>
<% } 
%>
<% 
if (debug) {
Set<String> tokens = message.getTokens();
for (String token : tokens){ 
%>
 	<div class="field">
	Token:	<%= token %>
	</div>
<br/>
<% } 
String contents = message.getAllTextContents();
%>
<div class="field">
Message Contents: <%= contents %>
</div>
<%}%>

	 <div class="x12" id="chart_div"></div> 
	 <div class="a1"></div>
	
<%  String debugLink = request.getRequestURL().toString() + "?msgId=" + request.getParameter("msgId") + "&debug=1"; %>
<a href=<%= debugLink %>>Show Message Tokens</a>
 </div>
</body> 
</html>